package xyz.keyon.utils;

import java.sql.*;

public class JdbcUtils {
    private static final String url = "jdbc:mysql://localhost:3306/servletdemotest?useSSL=true&useUnicode=true&characterEncoding=utf8";
    private static final String user = "root";
    private static final String password = "root";
    static{
        try{
            //导入驱动，加载Mysql驱动类
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        //与数据库建立连接
        return DriverManager.getConnection(url,user,password);
    }
    public static void release(ResultSet rs, Statement stmt, Connection conn){
        if(rs !=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stmt !=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //通用的数据库增删改工具
    public static int executeUpdate(String sql, Object[] params) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            //连接数据库
            conn = JdbcUtils.getConnection();
            //获取传输器对象,利用传输器对象执行语句
            pstmt = conn.prepareStatement(sql);
            for(int i = 0; i < params.length; ++i){
                pstmt.setObject(i+1,params[i]);
            }
            int count = pstmt.executeUpdate();
            if(count > 0){
                return 1;
            }else{
                return 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            //释放资源
            JdbcUtils.release(null, pstmt, conn);
        }
        return 1;
    }
    //通用的数据库查询工具
    public static ResultSet executeQuery(String sql, Object[] params) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            //连接数据库
            conn = JdbcUtils.getConnection();
            //获取传输器对象,利用传输器对象执行语句
            pstmt = conn.prepareStatement(sql);
            if(params != null){
                for(int i = 0; i < params.length; ++i){
                    pstmt.setObject(i+1,params[i]);
                }
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    //通用的查询表中记录的总数
    public static int getTotalCount(String sql) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int count = -1;
        try {
            //连接数据库
            conn = JdbcUtils.getConnection();
            //获取传输器对象，利用传输器对象执行语句
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            //处理返回结果
            if(rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.release(rs,pstmt,conn);
        }
        return count;
    }
}
